package com.xhu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhu.common.ResponseInfo;
import com.xhu.common.vo.EmpInfoVo;
import com.xhu.constant.ResultConstant;
import com.xhu.exceptions.UserLoginException;
import com.xhu.handler.ProfilePhotoHandler;
import com.xhu.mapper.DeptMapper;
import com.xhu.mapper.EmpInfoMapper;
import com.xhu.mapper.UserMapper;
import com.xhu.pojo.Dept;
import com.xhu.pojo.EmpInfo;
import com.xhu.pojo.User;
import com.xhu.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements IUserService {
    @Autowired()
    private UserMapper userMapper;

    @Autowired
    private EmpInfoMapper empInfoMapper;

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private ProfilePhotoHandler profilePhotoHandler;





    @Override
    public ResponseInfo<Map<String, Object>> login(String id, String password) {
        ResponseInfo<Map<String, Object>> responseInfo = new ResponseInfo<>();
        User user = userMapper.selectById(id);

        //认证
        if (!StringUtils.hasText(id) || !StringUtils.hasText(password)) {
            throw new UserLoginException("用户名或密码未填写或为空");
        }
        if (user == null) {
            throw new UserLoginException("用户不存在");
        }
        if (!(user.getEmpLoginid().equals(id) && user.getEmpPwd().equals(password))) {
            throw new UserLoginException("用户名与密码不匹配");
        }


        responseInfo.setStatus(ResultConstant.SUCCESS);
        responseInfo.setData(null);
        responseInfo.setMsg("登录成功");


        return responseInfo;


    }

    @Override
    public String getNameById(String id){
        User res = this.getById(id);
        if(res==null){
            throw new UserLoginException("查无此人");
        }
        return res.getEmpName();
    }

    @Override
    public List<User> getAllIdAndName(){
        List<User> list = list();
        //脱敏
        list.forEach(user -> user.setEmpPwd(null));

        return list;
    }

    @Override
    public EmpInfoVo getInfoById(String id){
        EmpInfo empInfoRes = empInfoMapper.selectById(id);

        User userRes = userMapper.selectById(empInfoRes.getEmpId());


        LambdaQueryWrapper<Dept> deptWrapper=new LambdaQueryWrapper<>();
        deptWrapper.eq(Dept::getDeptId, empInfoRes.getDept());
        Dept dept = deptMapper.selectOne(deptWrapper);

        //脱敏
        userRes.setEmpPwd(null);
        empInfoRes.setProfilePhotoUrl(null);

        return new EmpInfoVo()
                .setUser(userRes)
                .setEmpInfo(empInfoRes)
                .setDept(dept);
    }

    @Override
    public void updateInfo(EmpInfo empInfo){

        if (empInfo == null) {
            throw new UserLoginException("输入为空");
        }

        if (!StringUtils.hasText(empInfo.getEmpId())) {
            throw new UserLoginException("id为空");
        }

        empInfoMapper.updateById(empInfo);
    }

    @Override
    public void uploadProfilePhoto(MultipartFile file, String id) {
        String fileUrl = profilePhotoHandler.uplordFile(file);
        if (!StringUtils.hasText(fileUrl)) {
            throw new UserLoginException("头像上传失败");
        }

        LambdaUpdateWrapper<EmpInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(EmpInfo::getEmpId, id)
                .set(true, EmpInfo::getProfilePhotoUrl, fileUrl);
        empInfoMapper.update(new EmpInfo().setProfilePhotoUrl(fileUrl),wrapper);

    }

    @Override
    public String getPhotoUrl(String id){
        EmpInfo empInfo=empInfoMapper.selectById(id);
        return empInfo.getProfilePhotoUrl();
    }
}
